package com.authentications.security.handler;

import com.alibaba.fastjson.JSONObject;
import com.authentications.utils.ResultBean;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName JwtAuthenticationEntryPoint
 * @Author xiezhuocai
 * @Description TODO
 * @Date 2021/12/20 8:38
 */
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException authException) throws IOException, ServletException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        //String reason = "统一处理，原因：" + authException.getMessage();
        response.getWriter().write(JSONObject.toJSONString(ResultBean.error("System error",500,authException.getMessage())));
        response.getWriter().close();
        response.flushBuffer();
    }

}